package com.vcc.boss.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
import com.vcc.core.exception.ProgramException;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.model.file.cardholder.CardholderFilePojo;
import com.vcc.core.util.FileUtil;
import com.vcc.data.entity.Cardholder;
import com.vcc.service.boss.response.cardholder.CardholderVo;
import com.vcc.service.service.CardholderService;
import com.vcc.service.service.UserService;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("cardholder")
public class CardholderController {

    @Autowired
    private CardholderService cardholderService;
    @Autowired
    private UserService userService;

    @SaCheckPermission(value ="card:cardholder:list:view")
    @RestPack
    @SaCheckLogin
    @GetMapping("paging")
    public Page<CardholderVo> paging(Page<Cardholder> pageable, Long targetUserId, Long id) {
        if (targetUserId != null && targetUserId != 0) {
            Long dealerId = userService.getDealerIdById(targetUserId);
            if (!BossUserUtil.hasAllData() && !BossUserUtil.getId().equals(dealerId)) {
                throw new ProgramException("您没有权限操作该用户的卡信息");
            }
        }
        Page<Cardholder> paging = cardholderService.paging( pageable, targetUserId, id);
        return paging.map(CardholderVo::new);
    }

    @SaCheckPermission(value ="card:cardholder:list:derive")
    @RestPack
    @GetMapping("derive")
    public void derive(
            Long targetUserId,
            Long id,
            HttpServletResponse response
    ) {
        SXSSFWorkbook sxssfWorkbook = FileUtil.createCardholderTitle(1000);
        Page<Cardholder> paging;
        Long pages = 1L;
        do {
            paging = new Page<>(pages++, 20);
            Page<Cardholder> page = cardholderService.paging(paging, targetUserId, id);
            List<CardholderFilePojo> map = page.getRecords().stream().map(it -> {
                String account = userService.getAccountById(it.getUserId());
                return new CardholderFilePojo(
                        it.getId(),
                        account,
                        it.getFirstName(),
                        it.getLastName(),
                        it.getCountry(),
                        it.getProvince(),
                        it.getCity(),
                        it.getArea(),
                        it.getPostCode(),
                        it.getAddress(),
                        it.getPhone(),
                        it.getEmail(),
                        it.getRemark()
                );
            }).collect(Collectors.toList());
            FileUtil.deriveCardholderData(sxssfWorkbook, map,paging.getPageNumber(),paging.getPageSize());
        } while (!(pages).equals(paging.getTotalPage() + 1));
        String fileName = System.currentTimeMillis() + "持卡人信息.xlsx";
        FileUtil.downXlsxFile(response, sxssfWorkbook, fileName);
    }
}
